/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.statemachine.figure;

import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.FreeformViewport;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ScrollPane;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.figures.ElementFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * Figure of a Region in a state machine diagram.
 * 
 * @author David Servat
 * @since 29 jan. 07
 */
public class RegionFigure extends NodeFigure{
	
	/**
	 * Debug channel.
	 */
	private int channel = 501;

	/**
	 * 
	 */
	public static Color classColor = PapyrusPlugin.colorManager.get(new RGB(227,221,239));
	
	/**
	 * 
	 */
	public static Color classBorderColor= PapyrusPlugin.colorManager.get(new RGB(166,157,183));

	/**
	 * 
	 */
	protected UMLClassHeaderFigure headerFigure = null; //head of the package
	
	/**
	 * 
	 */
	protected UMLClassContentFigure contentFigure = null;// content of the package

	/**
	 * Specific flags to turn on/off disaply of a dashed border.
	 */
	private boolean displayBottomBorder = false;
	
	/**
	 * 
	 */
	private boolean displayRightBorder = false;


	/**
	 * 
	 */
	class UMLClassHeaderFigure extends ElementFigure{

		/**
		 * 
		 */
		public UMLClassHeaderFigure(){
			ToolbarLayout layout = new ToolbarLayout();
			layout.setStretchMinorAxis(false);
			setLayoutManager(layout);
			layout.setSpacing(2);
			this.borderColor = null;
			setOpaque(false);
		}

		/**
		 * setBounds is called to resize or move graphical Element by Eclipse
		 * In the case of this package we have to call setDimension Before.
		 * 
		 * @param rect coordinate of the contentPackage.
		 * 
		 * @see setDimension
		 */
		@Override
		public void setBounds(Rectangle rect){
			int headerHeight = 0;
			int headerWidth = rect.width;
			
//			int headerHeight = 20;
//			int headerWidth  = rect.width;
//			//calculate height
//			if(getStereotypesLabel() != null){
//				headerHeight = headerHeight + 20;
//			}
//			if(getQualifiedNameLabel() != null){
//				headerHeight = headerHeight + 20;
//			}
			Rectangle headerRect = new Rectangle();
			headerRect.setLocation(0, 0);
			headerRect.setSize(headerWidth, headerHeight);

			super.setBounds(headerRect);

			Debug.debug(this," bounds are : " +getBounds(), channel);
		}
		
		/**
		 * Overriden to get rid of the border.
		 * 
		 * @param border 
		 */
		@Override
		protected void paintBorder(Graphics graphics) {
		}
	}


	/**
	 * Displays the content of the region.
	 */
	class UMLClassContentFigure extends ElementFigure{

		/**
		 * 
		 */
		private IFigure pane;
		
		/**
		 * 
		 */
		protected ScrollPane scrollpane;

		/**
		 * 
		 */
		public UMLClassContentFigure(){
			Debug.debug(this,"  Call UMLPackageContentFigure", channel);

			//set content layout
			setLayoutManager(new StackLayout());
			//add pane for content
			pane = new FreeformLayer();
			pane.setLayoutManager(new FreeformLayout());

			//add scrollpane
			ScrollPane scrollpane = new ScrollPane();
			FreeformViewport viewport = new FreeformViewport();
			scrollpane.setViewport(viewport);
			scrollpane.setContents(pane);
			this.add(pane);

			//set colors
			setBackgroundColor(headerFigure.getBackgroundColor());
			setBorder(new LineBorder(headerFigure.getBorderColor()));
			setOpaque(false);
		}

		/**
		 * 
		 * 
		 * @return 
		 */
		@Override
		public IFigure getContentPane(){
			return pane;
		}

		/**
		 * setBound is called to resize or move graphical Element by Eclipse
		 * In the case of this package we have to call setDimension Before.
		 * 
		 * @param rect coordinate of the contentPackage.
		 * 
		 * @see setDimension
		 */
		@Override
		public void setBounds(Rectangle rect){
			super.setBounds(new Rectangle(0, headerFigure.getSize().height-1, rect.width, rect.height-headerFigure.getSize().height));

			Debug.debug(this," bounds are : " +getBounds(), channel);
		}

		/**
		 * Overriden to get rid of the border.
		 * 
		 * @param border 
		 */
		@Override
		protected void paintBorder(Graphics graphics) {
		}
	}

	/**
	 * 
	 */
	public RegionFigure(){
		ToolbarLayout layout = new ToolbarLayout();
		setLayoutManager(layout);

		headerFigure = new UMLClassHeaderFigure();
//		add(headerFigure);

		contentFigure =new UMLClassContentFigure();
		add(contentFigure);

		setOpaque(false);
	}

	/**
	 * 
	 * 
	 * @param backgroundColor 
	 * 
	 * @see com.cea.papyrus.common.figures.AbstractElementFigure#setBackgroundColor(org.eclipse.swt.graphics.Color)
	 */
	@Override
	public void setBackgroundColor(Color backgroundColor){
		super.setBackgroundColor(backgroundColor);
		headerFigure.setBackgroundColor(backgroundColor);
	}

	/**
	 * 
	 * 
	 * @param color 
	 * 
	 * @see com.cea.papyrus.common.figures.AbstractElementFigure#setFontColor(org.eclipse.swt.graphics.Color)
	 */
	@Override
	public void setFontColor(Color color){
		this.fontColor=color;
		getHeader().setFontColor(color);
	}

	/**
	 * 
	 * 
	 * @param color 
	 * 
	 * @see com.cea.papyrus.common.figures.AbstractElementFigure#setForeGroundColor(org.eclipse.swt.graphics.Color)
	 */
	@Override
	public void setForeGroundColor(Color color){
		super.setForeGroundColor(color);
		contentFigure.setBackgroundColor(color);
	}

	/**
	 * setBound is called to resize or move graphical Element by Eclipse
	 * It call setBound of each subFigure.
	 * In the case of contentPackage we call setDimension
	 * 
	 * @param rect coordinate of the contentPackage.
	 * 
	 * @see setDimension
	 */
	@Override
	public void setBounds(Rectangle rect){
		//header
		headerFigure.setBounds(new Rectangle(rect));
		//content
		contentFigure.setBounds(new Rectangle(rect));

		//specific part
		//changes of the size of the rounded corners
		super.setBounds(new Rectangle(rect));
		corner = new Dimension(this.getBounds().getSize());
		corner.height /= 10;
		corner.width /= 10;

		Debug.debug(this," bounds are : " +getBounds(), channel);
	}

	/**
	 * to obtain the header of the package figure.
	 * 
	 * @return IFigure the header
	 */
	public UMLClassHeaderFigure getHeader(){
		return headerFigure;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public IFigure getContentsPane(){
		return contentFigure.getContentPane();
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected boolean useLocalCoordinates(){
		return true;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public int getHeadWidth(){
		return headerFigure.getSize().width;
	}
	
	/**
	 * 
	 * 
	 * @return 
	 */
	public int getHeadHeight(){
		return headerFigure.getSize().height;
	}

	/**
	 * 
	 * 
	 * @return Returns the nameLabel.
	 */
	@Override
	public Label getNameLabel(){
		return headerFigure.getNameLabel();
	}

	/**
	 * 
	 * 
	 * @return Returns the qualifiedNameLabel.
	 */
	@Override
	public Label getQualifiedNameLabel(){
		return headerFigure.getQualifiedNameLabel();
	}

	/**
	 * 
	 * 
	 * @return Returns the nameLabel.
	 */
	@Override
	public Label getStereotypesLabel(){
		return contentFigure.getStereotypesLabel();
	}

	/**
	 * 
	 * 
	 * @param name 
	 */
	@Override
	public void setName(String name){
		headerFigure.setName(name);
	}

	/**
	 * 
	 * 
	 * @param packageName 
	 */
	@Override
	public void setQualifiedName(String packageName){
		headerFigure.setQualifiedName(packageName);
	}

	/**
	 * Set the stereotype value.
	 * If param is null, remove the label.
	 * 
	 * @param stereotypes 
	 */
	@Override
	public void setStereotypes(String stereotypes){
		headerFigure.setStereotypes(stereotypes);
	}

	/**
	 * Refresh presentation of current figure
	 * with presentation specified in a GraphNode.
	 * 
	 * @param presentation 
	 * @param selection 
	 */
	@Override
	public void refreshPresentation(String presentation, Element selection, GraphElement ge){
		headerFigure.refreshPresentation(presentation, selection, ge);
	}

	/**
	 * Refresh list of stereotypes of current EditPart if required
	 * either with or without the first stereotype.
	 * 
	 * @param presentation 
	 * @param hasIcon 
	 * @param hasShape 
	 * @param stereotypes 
	 */
	@Override
	public void refreshStereotypes(String stereotypes, String presentation, boolean hasIcon, boolean hasShape){
		headerFigure.refreshStereotypes(stereotypes, presentation, hasIcon, hasShape);
	}

	/**
	 * Specific flags to turn on/off disaply of a dashed border.
	 * 
	 * @param b 
	 */
	public void setDisplayBottomBorder(boolean b){
		displayBottomBorder = b;
	}

	/**
	 * Specific flags to turn on/off disaply of a dashed border.
	 * 
	 * @param b 
	 */
	public void setDisplayRightBorder(boolean b){
		displayRightBorder = b;
	}
	
	@Override
	protected void paintBorder(Graphics graphics) {
	}

	/**
	 * Provides for a specific graphical representation with added dashed borders
	 * in case region is surrounded with other regions.
	 * 
	 * @param graphics 
	 */
	@Override
	public void paintFigure(Graphics graphics){
			graphics.setForegroundColor(getBorderColor());
			Rectangle rect = getBounds();
			//the specific drawing of borders
			if(displayRightBorder){
				graphics.setLineStyle(SWT.LINE_DASH);
				graphics.drawLine(rect.x+rect.width-1, rect.y-2, rect.x+rect.width-1, rect.y-2+rect.height);
			}
			if(displayBottomBorder){
				graphics.setLineStyle(SWT.LINE_DASH);
				graphics.drawLine(rect.x-2, rect.y+rect.height-1, rect.x-2+rect.width, rect.y+rect.height-1);
			}
	}
}
